home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / libpcap-0.0 / scanner.l < prev    next >
Encoding:
Lex Description  |  1995-04-03  |  4.1 KB  |  195 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: scanner.l,v 1.40 94/06/10 17:21:44 mccanne Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30.  
  31. #include <ctype.h>
  32. #include <unistd.h>
  33. #include <pcap.h>
  34. #include <pcap-namedb.h>
  35.  
  36. #include "gencode.h"
  37. #include "tokdefs.h"
  38.  
  39. #ifndef __GNUC__
  40. #define inline
  41. #endif
  42.  
  43. static int stoi(char *);
  44. static inline int xdtoi(int);
  45.  
  46. #ifdef FLEX_SCANNER
  47. #undef YY_INPUT
  48. #define YY_INPUT(buf, result, max)\
  49.  {\
  50.     char *src = in_buffer;\
  51.     int i;\
  52. \
  53.     if (*src == 0)\
  54.         result = YY_NULL;\
  55.     else {\
  56.         for (i = 0; *src && i < max; ++i)\
  57.             buf[i] = *src++;\
  58.         in_buffer += i;\
  59.         result = i;\
  60.     }\
  61.  }
  62. #else
  63. #undef getc
  64. #define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
  65. #endif
  66.  
  67. #define yylval pcap_lval
  68. extern YYSTYPE yylval;
  69.  
  70. static char *in_buffer;
  71.  
  72. %}
  73.  
  74. N        ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
  75. B        ([0-9A-Fa-f][0-9A-Fa-f]?)
  76.  
  77. %a 3000
  78.  
  79. %%
  80. dst        return DST;
  81. src        return SRC;
  82.  
  83. link|ether|ppp|slip  return LINK;
  84. fddi        return LINK;
  85. arp        return ARP;
  86. rarp        return RARP;
  87. ip        return IP;
  88. tcp        return TCP;
  89. udp        return UDP;
  90. icmp        return ICMP;
  91.  
  92. decnet        return DECNET;
  93. lat        return LAT;
  94. moprc        return MOPRC;
  95. mopdl        return MOPDL;
  96.  
  97. host        return HOST;
  98. net        return NET;
  99. port        return PORT;
  100. proto        return PROTO;
  101.  
  102. gateway        return GATEWAY;
  103.  
  104. less        return LESS;
  105. greater        return GREATER;
  106. byte        return BYTE;
  107. broadcast    return TK_BROADCAST;
  108. multicast    return TK_MULTICAST;
  109.  
  110. and|"&&"    return AND;
  111. or|"||"        return OR;
  112. not        return '!';
  113.  
  114. len|length    return LEN;
  115. inbound        return INBOUND;
  116. outbound    return OUTBOUND;
  117.  
  118. [ \n\t]            ;
  119. [+\-*/:\[\]!<>()&|=]    return yytext[0];
  120. ">="            return GEQ;
  121. "<="            return LEQ;
  122. "!="            return NEQ;
  123. "=="            return '=';
  124. "<<"            return LSH;
  125. ">>"            return RSH;
  126. {N}            { yylval.i = stoi((char *)yytext); return NUM; }
  127. ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})    {
  128.             yylval.s = sdup((char *)yytext); return HID;
  129. }
  130. {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
  131.               return EID; }
  132. {B}:+({B}:+)+        { bpf_error("bogus ethernet address %s", yytext); }
  133. [A-Za-z][-_.A-Za-z0-9]*    { yylval.s = sdup((char *)yytext); return ID; }
  134. "\\"[^ !()\n\t]+    { yylval.s = sdup((char *)yytext + 1); return ID; }
  135. [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+    { bpf_error("illegal token: %s\n", yytext); }
  136. .            { bpf_error("illegal char '%c'", *yytext); }
  137. %%
  138. void
  139. lex_init(buf)
  140.     char *buf;
  141. {
  142.     in_buffer = buf;
  143. }
  144.  
  145. /*
  146.  * Also define a yywrap.  Note that if we're using flex, it will
  147.  * define a macro to map this identifier to pcap_wrap.
  148.  */
  149. int
  150. yywrap()
  151. {
  152.     return 1;
  153. }
  154.  
  155. /* Hex digit to integer. */
  156. static inline int
  157. xdtoi(c)
  158.     register int c;
  159. {
  160.     if (isdigit(c))
  161.         return c - '0';
  162.     else if (islower(c))
  163.         return c - 'a' + 10;
  164.     else
  165.         return c - 'A' + 10;
  166. }
  167.  
  168. /*
  169.  * Convert string to integer.  Just like atoi(), but checks for
  170.  * preceding 0x or 0 and uses hex or octal instead of decimal.
  171.  */
  172. static int
  173. stoi(s)
  174.     char *s;
  175. {
  176.     int base = 10;
  177.     int n = 0;
  178.  
  179.     if (*s == '0') {
  180.         if (s[1] == 'x' || s[1] == 'X') {
  181.             s += 2;
  182.             base = 16;
  183.         }
  184.         else {
  185.             base = 8;
  186.             s += 1;
  187.         }
  188.     }
  189.     while (*s)
  190.         n = n * base + xdtoi(*s++);
  191.  
  192.     return n;
  193. }
  194.  
  195.